package com.ddpt.gateway.filter;

import com.ddpt.gateway.filter.base.BaseLimitFilter;
import com.ddpt.gateway.filter.config.LimitConfiguration;
import com.ddpt.platform.common.constant.GlobalConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * header头参数传递
 */
@Slf4j
@Component
@RefreshScope
public class HeaderFillFilter extends BaseLimitFilter implements GlobalFilter, Ordered {

    @Autowired
    private LimitConfiguration configuration;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest()
                .mutate()
                .header(GlobalConstant.header_enable_secret, configuration.isEnableSecret() + "")
                .header(GlobalConstant.header_secret_key, configuration.getSecretKey())
                .build();
        return chain.filter(exchange.mutate().request(request).build());
    }

    @Override
    public int getOrder() {
        return 96;
    }
}
